package coroutines.coroutine

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.lang.Exception

class FirstCoroutineTest {

}

fun main() {
    /**
     * 不设置调度器上下文，默认调用和Dispatchers.IO和Dispatchers.Default共用的线程池的线程
     * */
    GlobalScope.launch {
        delay(1000)
        println("Not set dispatcher, ${Thread.currentThread().name} GlobalScope.launch()")
    }

    GlobalScope.launch(context = Dispatchers.Default) {
        delay(900)
        println("Set dispatcher Default, ${Thread.currentThread().name} GlobalScope.launch()")
    }


    GlobalScope.launch(context = Dispatchers.Unconfined) {
        delay(200)
        println("Set dispatcher Unconfined, ${Thread.currentThread().name} GlobalScope.launch()")
    }

    GlobalScope.launch(context = Dispatchers.IO) {
        delay(1000)
        println("Set dispatcher IO, ${Thread.currentThread().name} GlobalScope.launch()")
    }

//    GlobalScope.launch(context = Dispatchers.Main) {
//        delay(200)
//        println("${Thread.currentThread().name} GlobalScope.launch()")
//    }

    try {
        Thread.sleep(1000)
    } catch (e: Exception) {
        e.printStackTrace()
    }

    println("Main thread ${Thread.currentThread().name} completed")
}